package com.fuyin.commons.response;

import com.fuyin.commons.enums.ErrorCodeEnum;
import lombok.Data;

import java.beans.ConstructorProperties;
import java.io.Serializable;

/**
 * @author yuanxilan (yuanxilan@keep.com)
 * @Description
 * @date 2021年10月23日 2:20 下午
 * @since
 */
@Data
public class BaseResponse<T extends Serializable> implements Serializable {

    private boolean ok;

    private T data;

    private Integer errorCode;

    private String text;

    public String toString() {
        return "BaseResponse(ok=" + this.isOk() + ", data=" + this.getData() + ", errorCode=" + this.getErrorCode() + ", text=" + this.getText() + ")";
    }

    @ConstructorProperties({"ok", "data", "errorCode", "text"})
    public BaseResponse(boolean ok, T data, Integer errorCode, String text) {
        this.ok = ok;
        this.data = data;
        this.errorCode = errorCode;
        this.text = text;
    }

    public BaseResponse() {
    }

    public static BaseResponse success() {
        return new BaseResponse(true, "", 200, "");
    }

    public static <T extends Serializable> BaseResponse<T> success(T data) {
        return new BaseResponse<>(true, data, 200, "");
    }

    public static BaseResponse error(Integer errorCode, String text) {
        return new BaseResponse(true, "", errorCode, text);
    }

    public static BaseResponse error(ErrorCodeEnum errorCodeEnum) {
        return new BaseResponse(true, "", errorCodeEnum.getErrorCode(), errorCodeEnum.getErrorMessage());
    }
}
